package com.enniswu.weitter.browse;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.enniswu.weitter.R;
import com.github.chrisbanes.photoview.PhotoView;

import java.util.List;

public class BrowseAdapter extends PagerAdapter {

    private Activity activity;
    private List<String> album;

    public BrowseAdapter(Activity activity, List<String> album) {
        this.activity = activity;
        this.album = album;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        String image = album.get(position);
        View view = LayoutInflater.from(activity).inflate(R.layout.item_photo, container, false);
        PhotoView photoView = view.findViewById(R.id.photoView);
        Glide.with(activity).load(image).into(photoView);
        container.addView(view);
        view.setOnClickListener(clickListener);
        photoView.setOnClickListener(clickListener);
        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }

    @Override
    public int getCount() {
        return album.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            activity.finish();
        }
    };
}
